<template>
  <el-dialog :visible="showRoleDialog" title="分配角色" append-to-body @close="hClose">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <!-- // 分配角色 -->
    <el-row>
      <div style="margin-top: 20px; text-align: right">
        <el-button type="primary" @click="hSave">确定</el-button>
        <el-button @click="hClose">取消</el-button>
      </div>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRolesEnadled, assignRoles } from '@/api/role'
import { getEmployeeDetail } from '@/api/employee'

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      roleList: [], // 所有角色数据
      roleIds: []

    }
  },
  created() {
    this.loadRose()
  },
  methods: {
    async hSave() {
      await assignRoles({ id: this.curId, roleIds: this.roleIds })
      this.$message.success('恭喜您，角色分配成功')
      this.hClose()
    },
    async getRolesById() {
    //   console.log(this.curId)
      // 请求接口，获取当前员工信息
      // console.log('1')
      const res = await getEmployeeDetail(this.curId)
      //   console.log('获取当前员工信息', res.data.roleIds)
      // 数据回填
      this.roleIds = res.data.roleIds
    },

    hClose() {
      this.$emit('update:showRoleDialog', false)
    },
    async loadRose() {
      const res = await getRolesEnadled()
      // console.log('获取角色列表', res)
      this.roleList = res.data
    }
  }
}
</script>

<style>

</style>
